package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.IeoConfig;
import com.novax.ex.activity.infrastructure.entity.IeoOrder;
import com.novax.ex.activity.open.api.inner.IeoInnerOrderApi;
import com.novax.ex.activity.open.model.query.OrderDistributionQuery;
import com.novax.ex.activity.open.model.request.IeoOrderUpdateRequest;
import com.novax.ex.activity.open.model.response.IeoDistributionResponse;
import com.novax.ex.activity.provider.common.Operation;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.IeoConfigService;
import com.novax.ex.activity.provider.service.IeoDistributionService;
import com.novax.ex.activity.provider.service.IeoOrderService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.core.redis.RedisUtil;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description  订单管理 后台
 * @Author ChenXi
 * @Date 2023/03/22/18:01
 */

@RestController
public class IeoInnerOrderController extends BaseActivityController implements IeoInnerOrderApi {

    @Resource
    private IeoOrderService ieoOrderService;
    @Resource
    private IeoConfigService ieoConfigService;
    @Resource
    private IeoDistributionService ieoDistributionService;

    @Override
    public ReturnResult<?> modify(String language, List<IeoOrderUpdateRequest> ieoOrderList) {
        if(ieoOrderList.isEmpty()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        return ieoOrderService.modifyAmountByIds(language, ieoOrderList);

    }

    @Override
    public ReturnResult<?> unfree(String uri, String language, Long ieoId) {
        return ieoOrderService.unfree(uri, language, ieoId);
    }

    @Override
    public ReturnResult<?> stages(String uri, String language, Long ieoId) {
        return ieoOrderService.stages(uri, language, ieoId);
    }

    @Override
    public ReturnResult<?> reissue(String uri, String language, Long id) {
        return ieoOrderService.reissue(uri, language, id);
    }

    @Override
    public ReturnResult<PageUtil<IeoDistributionResponse>> distributionPage(String language, OrderDistributionQuery query) {
        String[] userIdStrList = Optional.ofNullable(query.getUserIdListString())
            .filter(StringUtils::isNoneBlank)
            .map(s -> s.split(","))
            .orElseGet(() -> new String[0]);
        if(userIdStrList.length > 0){
            query.setUserIdList(Arrays.stream(userIdStrList).map(Long::parseLong).collect(Collectors.toList()));
        }
        PageUtil<IeoDistributionResponse> pageInfo = new PageUtil<>(query);
        // 根据分期期数排序
        pageInfo.setOrder("order by order_id asc, number asc");
        pageInfo.setCondition(Collections.singletonMap("query", query));
        ieoDistributionService.findPage(pageInfo, IeoDistributionResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pageInfo);
    }

    @Override
    public ReturnResult<List<String>> queryBox(String language) {
        List<String> queryBoxList = ieoConfigService.queryBox();
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), queryBoxList);
    }

}
